﻿using System.Collections.Specialized;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;

namespace Ibatis.Test.Maps
{
    public class SqliteMapper
    {
        private static volatile ISqlMapper _mapper = null;
        private static object obj = new object();

        protected static void Configure(object obj)
        {
            _mapper = null;
        }

        protected static void InitMapper()
        {
            ConfigureHandler handler = new ConfigureHandler(Configure);
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            NameValueCollection properties = new NameValueCollection
            {
                {"connectionString", "Data Source=D:\\AppData\\nhibernate.db;"}
            };
            builder.Properties = properties;
            _mapper = builder.ConfigureAndWatch("SqliteMap.config", handler);
        }

        public static ISqlMapper Instance()
        {
            if (_mapper == null)
            {
                lock (obj)
                {
                    if (_mapper == null) // double-check
                    {
                        InitMapper();
                    }
                }
            }
            return _mapper;
        }

        public static ISqlMapper Get()
        {
            return Instance();
        }
    }
}
